package com.ftar.urgent.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.ftar.urgent.viewmodel.CommonEvent;
import com.ftar.urgent.viewmodel.SP_Constants;
import com.ftar.urgent.voice.OfflineWords;
import com.ftar.urgent.voice.OnLineAsrManager;
import com.ftar.voicecommand.FTVoiceEngine;
import com.ftar.voicecommand.audiorecord.FTAudioRecordCallback;

import org.greenrobot.eventbus.EventBus;

/**
 * @author: wei.jin
 * @date: 2023/6/1
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class AsrService extends Service {
    private String TAG = "AsrService";
    private Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();

    }


    /**
     * Init offline command
     */
    private void initVoiceInstruction(FTVoiceEngine.FTOfflineCommandCallback commandCallback, FTAudioRecordCallback audioRecordCallback) {
        //语音识别离线文字
        FTVoiceEngine.getInstance().init(this);
        FTVoiceEngine.getInstance().updateOfflineWords(OfflineWords.getFTOfflineWords(commandCallback));
        FTVoiceEngine.getInstance().setOnAudioRecordCallback(audioRecordCallback);
        FTVoiceEngine.getInstance().startVoice();
    }

    /**
     * Offline asr key word
     */
    private FTVoiceEngine.FTOfflineCommandCallback ftOfflineCommandCallback = new FTVoiceEngine.FTOfflineCommandCallback() {
        @Override
        public void onOfflineCommandEvent(String word) {
            Log.d(TAG, "offline command:" + word);
            ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName componentName = activityManager.getRunningTasks(1).get(0).topActivity;
            CommonEvent commonEvent = new CommonEvent(SP_Constants.EVENT_COMMAND_ASR);
            commonEvent.setData(word);
            commonEvent.setmClass(componentName.getClassName());
            EventBus.getDefault().post(commonEvent);
        }
    };

    private FTAudioRecordCallback ftAudioRecordCallback = new FTAudioRecordCallback() {
        @Override
        public void onDataRecv(byte[] bytes, int length, int volume) {
            OnLineAsrManager.getInstance().sendData(bytes, volume);
        }
    };

    /**
     * init online asr, asr content from unicom
     */
    private void initOnlineAsr() {
//        if(mOnLineAsrManager == null) {
//            mOnLineAsrManager = new OnLineAsrManager();
//            mOnLineAsrManager.startAsr(new OnLineAsrManager.OnAsrResultListener() {
//                @Override
//                public void onResult(String result) {
//                    Log.d(TAG, "onLine result:" + result);
//                    JSONObject jo=JSONObject.parseObject(result);
//                    JSONObject rj = jo.getJSONObject("result");
//
//                    String text = rj.getString("text");
//                    if(jo.getBooleanValue("is_endpoint")) {
//                    } else {
//                    }
//                }
//            });
//        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //return super.onStartCommand(intent, flags, startId);
        Log.d(TAG, "onStartCommand");
        initVoiceInstruction(ftOfflineCommandCallback, ftAudioRecordCallback);
        initOnlineAsr();

        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
